package ast;

import java.io.PrintStream;
import java.util.Scanner;
import java.util.Map;
import java.util.Stack;

public class PrintStmt extends Stmt {
    public final Expr expr;

    public PrintStmt(Expr e, Location loc) {
        super(loc);
        expr = e;
    }

    public void print(PrintStream ps, String indent) {
        ps.print(indent + "print ");
        expr.print(ps);
        ps.print(";");
    }

    public void print(PrintStream ps) {
        print(ps, "");
    }

    // interpret: print the value of the expression
    public void execute(Stack<Map<String, AbstractValue>> state, Scanner scanner) throws Exception {
        AbstractValue value = expr.evaluate(state, scanner);
        // to check type of expression
        // if (value instanceof Double) {
        //     // Format float (double) numbers to 1 decimal place
        //     System.out.println(String.format("%.2f", value.doubleValue()));
        // } else {
        //     // For integers, just print normally
        //     System.out.println(value);
        // }
        System.out.println(value.getTypeStr());
        return ;
    }
}
